﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Processes.Orden;
using System.Web.Security;
using Model.Orden;
using System.IO;
using iTextSharp.text;
using iTextSharp.text.pdf; 

namespace Interfaces.Acta
{
    public partial class VerActa : System.Web.UI.Page
    {
        string nro;
        DateTime date;
        GestorOrden gestorOrden;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!User.Identity.IsAuthenticated)
                FormsAuthentication.RedirectToLoginPage();
            gestorOrden = new GestorOrden();
            if (!IsPostBack)
            {
                try
                {

                    Session["Nro"] = Request.QueryString["Nro"];
                    Session["Date2"] = Request.QueryString["Date"];
                    nro = Session["Nro"].ToString();
                    date = DateTime.Parse(Session["Date2"].ToString());
                    FechaLabel.Text += date.ToShortDateString();
                    NroLabel.Text += nro;
                    List<Punto> puntos = new List<Punto>();
                    puntos = gestorOrden.puntosDeNro(nro);
                    Session["puntosAImprimir"] = puntos;
                    DataList1.DataSource = puntos;
                    DataList1.DataBind();
                }
                catch (Exception ex)
                {
                    LabelError.Text = "No se pudo cargar el acta, hubo un error.";
                }

            }
            if (Session["Nro"] != null)
            {
                nro = Session["Nro"].ToString();
            }
            if (Session["Date2"] != null)
            {
                try
                {
                    date = DateTime.Parse(Session["Date2"].ToString());
                }
                catch (Exception ex)
                {

                    LabelError.Text = "No se reconoce la fecha";
                }

            }
        }
        public void llenarPuntos(List<Punto> puntos)
        {

        }
        protected void Page_PreInit(object sender, EventArgs e)
        {
            if (Session["BuscarMaster"] != null)
            {
                string res = (string)Session["BuscarMaster"];
                this.MasterPageFile = res;
            }
        }

        protected void ButtonImprimir_Click(object sender, EventArgs e)
        {

            // Create a Document object
            List<Punto> puntos = new List<Punto>();
            puntos = (List<Punto>)Session["puntosAImprimir"];
            var document = new Document(PageSize.A4, 50, 50, 25, 25);

            // Create a new PdfWriter object, specifying the output stream
            var output = new FileStream(Server.MapPath("MyFirstPDF.pdf"), FileMode.Create);
            var writer = PdfWriter.GetInstance(document, output);
            iTextSharp.text.Image portada = iTextSharp.text.Image.GetInstance(Server.MapPath("~/Images/PortadaPDF.bmp"));
            portada.Alignment = iTextSharp.text.Image.ALIGN_CENTER;
            // Open the Document for writing
            document.Open();
            document.Add(portada);
            Chunk chunkFecha = new Chunk(Session["Date2"].ToString(),FontFactory.GetFont("Calibri",11,iTextSharp.text.Font.NORMAL));
            
            var fecha = new Paragraph(chunkFecha);
            fecha.Alignment = iTextSharp.text.Image.ALIGN_CENTER;
            // Create a new Paragraph object with the text, "Hello, World!"
            document.Add(fecha);
            Chunk chunkNroActa = new Chunk("Acta N° "+Session["Nro"].ToString(),FontFactory.GetFont("Calibri",16,iTextSharp.text.Font.BOLD));
            var nro = new Paragraph(chunkNroActa);
            nro.Alignment = iTextSharp.text.Image.ALIGN_CENTER;
            document.Add(nro);
            for (int i = 0; i < puntos.Count; i++)
            {
                Chunk chunkPunto = new Chunk(puntos[i].NumeroPunto+" "+puntos[i].NombreDePunto,FontFactory.GetFont("Calibri",11,iTextSharp.text.Font.BOLD));
                var welcomeParagraph = new Paragraph(chunkPunto);
                document.Add(welcomeParagraph);
                welcomeParagraph = new Paragraph(puntos[i].Contenido);
                document.Add(welcomeParagraph);
            }
            

            // Close the Document - this saves the document contents to the output stream
            document.Close();
            Response.Redirect("MyFirstPDF.pdf");
        }
    }
}